/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
/// <summary>
/// Summary description for cmsProductPropertyDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class cmsProductPropertyDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public cmsProductPropertyDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(cmsProductPropertyDO objcmsProductPropertyDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@PropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.PropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Value", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsProductPropertyDO.Value;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(cmsProductPropertyDO objcmsProductPropertyDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductPropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductPropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@PropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.PropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Value", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsProductPropertyDO.Value;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }
        public int UpdateOrInsertExcel(string productCode, string propertyCode,string value)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_InsertOrUpdate";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductCode", SqlDbType.NVarChar);
            Sqlparam.Value = productCode;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@PropertyCode", SqlDbType.NVarChar);
            Sqlparam.Value = propertyCode;
            Sqlcomm.Parameters.Add(Sqlparam);


            Sqlparam = new SqlParameter("@Value", SqlDbType.NVarChar);
            Sqlparam.Value = value;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }
        public int UpdateOrInsert(cmsProductPropertyDO objcmsProductPropertyDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_UpdateOrInsert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@PropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.PropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Value", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsProductPropertyDO.Value;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;
        }

        public int Delete(cmsProductPropertyDO objcmsProductPropertyDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ProductPropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductPropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }
        public int DeleteByProductID(int productID)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_DeleteByProductID";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = productID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }
        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public cmsProductPropertyDO Select(cmsProductPropertyDO objcmsProductPropertyDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ProductPropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductPropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["ProductPropertyID"]))
                    objcmsProductPropertyDO.ProductPropertyID = Convert.ToInt32(dr["ProductPropertyID"]);
                if (!Convert.IsDBNull(dr["ProductID"]))
                    objcmsProductPropertyDO.ProductID = Convert.ToInt32(dr["ProductID"]);
                if (!Convert.IsDBNull(dr["PropertyID"]))
                    objcmsProductPropertyDO.PropertyID = Convert.ToInt32(dr["PropertyID"]);
                if (!Convert.IsDBNull(dr["Value"]))
                    objcmsProductPropertyDO.Value = Convert.ToString(dr["Value"]);

            }
            return objcmsProductPropertyDO;
        }

        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrcmsProductPropertyDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cmsProductPropertyDO objcmsProductPropertyDO = new cmsProductPropertyDO();
                    if (!Convert.IsDBNull(dr["ProductPropertyID"]))
                        objcmsProductPropertyDO.ProductPropertyID = Convert.ToInt32(dr["ProductPropertyID"]);
                    if (!Convert.IsDBNull(dr["ProductID"]))
                        objcmsProductPropertyDO.ProductID = Convert.ToInt32(dr["ProductID"]);
                    if (!Convert.IsDBNull(dr["PropertyID"]))
                        objcmsProductPropertyDO.PropertyID = Convert.ToInt32(dr["PropertyID"]);
                    if (!Convert.IsDBNull(dr["Value"]))
                        objcmsProductPropertyDO.Value = Convert.ToString(dr["Value"]);
                    arrcmsProductPropertyDO.Add(objcmsProductPropertyDO);
                }
            }
            return arrcmsProductPropertyDO;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable SelectByProductID(int productID)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_GetByFK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = productID;
            Sqlcomm.Parameters.Add(Sqlparam);
            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable SelectByProductIDAndCate(int productID, int productCategoryID)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProperty_SelectByProductCategoryAndProduct";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = productID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ProductCategoryID", SqlDbType.Int);
            Sqlparam.Value = productCategoryID;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public cmsProductPropertyDO SelectByFK(cmsProductPropertyDO objcmsProductPropertyDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsProductProperty_SelectByFK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@PropertyID", SqlDbType.Int);
            Sqlparam.Value = objcmsProductPropertyDO.PropertyID;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["ProductPropertyID"]))
                    objcmsProductPropertyDO.ProductPropertyID = Convert.ToInt32(dr["ProductPropertyID"]);
                if (!Convert.IsDBNull(dr["ProductID"]))
                    objcmsProductPropertyDO.ProductID = Convert.ToInt32(dr["ProductID"]);
                if (!Convert.IsDBNull(dr["PropertyID"]))
                    objcmsProductPropertyDO.PropertyID = Convert.ToInt32(dr["PropertyID"]);
                if (!Convert.IsDBNull(dr["Value"]))
                    objcmsProductPropertyDO.Value = Convert.ToString(dr["Value"]);

            }
            return objcmsProductPropertyDO;
        }

        #endregion

    }

}
